package com.homer.gps.track.adapters;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.homer.gps.track.R;
import com.homer.gps.track.helper.UsefullStaff;
import com.homer.gps.track.persist.DatabaseHelper;
import com.homer.gps.track.persist.Repository;
import com.homer.gps.track.persist.RouteModel;
import com.homer.gps.track.persist.StopModel;
import com.j256.ormlite.android.apptools.OpenHelperManager;

public class RoutesArrayAdapter extends ArrayAdapter<RouteModel>{
	
	private final String LOG_TAG = "RoutesAdapter";

	private List<RouteModel> routes = null;
	private Activity context = null;
	private boolean deleteMode = false;
	
	private Repository repository;
	private DatabaseHelper dbHelper = null;
	
	public RoutesArrayAdapter(final Activity context, final int textViewResourceId, final List<RouteModel> routes) {
		super(context, textViewResourceId, routes);
		this.context = context;
		this.routes = routes;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = convertView;
		
		this.repository = new Repository(getHelper());
		
		if (rowView == null) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
			rowView = inflater.inflate(R.layout.routes_list_item, parent, false);
		}
		
		TextView routeName = (TextView) rowView.findViewById(R.id.route_name);
		TextView routeStartDate = (TextView) rowView.findViewById(R.id.route_start_date);
		ImageView typePicture = (ImageView) rowView.findViewById(R.id.type_picture);
		RouteModel route = (RouteModel)routes.get(position);
		
		routeName.setText(String.valueOf(route.getName()));
		List<StopModel> startStops = new ArrayList<StopModel>();
		try {
			startStops = repository.lookupStopsForRoute(route, StopModel.STOP_TYPE_START);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (startStops != null) {
			routeStartDate.setText(UsefullStaff.getFormatedDateAndTime(startStops.get(0).getStartDateTime(), "dd/MM/yyyy HH:mm"));
		}
		
		ImageView deleteBt = (ImageView )rowView.findViewById(R.id.delete_bt);
		deleteBt.setTag(route);
		if (deleteMode) {
			deleteBt.setVisibility(View.VISIBLE);
		} else {
			deleteBt.setVisibility(View.INVISIBLE);
		}
		
		if (route.isRunning()) {
			rowView.setBackgroundResource(R.color.grey);
		}
		
//		int routeType = Integer.parseInt(route.getType());		
		switch (route.getType()) {
		case 0:
			typePicture.setImageResource(R.drawable.route_type_personal);
			break;
		case 1:
			typePicture.setImageResource(R.drawable.route_type_bussiness);
			break;
		case 2:
			typePicture.setImageResource(R.drawable.route_type_other);
			break;
		default:
			break;
		}
		
		return rowView;
	}
	
	public void setDeleteMode(boolean value) {
		deleteMode = value;
		notifyDataSetChanged();	
	}
	
	private DatabaseHelper getHelper() {
	    if (dbHelper == null) {
	        dbHelper = (DatabaseHelper)OpenHelperManager.getHelper(context, DatabaseHelper.class);
	    }
	    return dbHelper;
	}
}
